package com.yonyou.pmclouds.repeatwork.process;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.yonyou.pmclouds.basecom.billstatus.BillStatusConst;
import com.yonyou.pmclouds.basecom.bp.processor.SingleProcessor;
import com.yonyou.pmclouds.basecom.exception.BusinessException;
import com.yonyou.pmclouds.basecom.util.DateUtils;
import com.yonyou.pmclouds.project.entity.ProjectVO;
import com.yonyou.pmclouds.project.rmiitf.ProjectMaintain;
import com.yonyou.pmclouds.project.rmiitf.ProjectSimpleQuery;
import com.yonyou.pmclouds.repeatwork.entity.RepeatWorkVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * 计算复工单对应项目累计停工时间
 */
@Component
public class CaculateStopDaysProcessor implements SingleProcessor<RepeatWorkVO> {
    @Autowired
    private ProjectSimpleQuery projectSimpleQuery;
    @Autowired
    private ProjectMaintain projectMaintain;


    @Override
    public void process(RepeatWorkVO vo, RepeatWorkVO orig) throws BusinessException {
        //审批未通过
        if(!vo.getBillstatus().equals(BillStatusConst.COMMON_APPROVED_STATUS)){
            return ;
        }
        //查询项目档案上次的累计停工天数。
        String pk_project = vo.getPkProject();
        ProjectVO projectVO = projectSimpleQuery.queryProject(pk_project);
        Integer totworkStopDays = projectVO.getTotworkstopDays()==null?0:projectVO.getTotworkstopDays();
        //查询和当前复工单对应的停工单，然后计算本次停工天数。
        Date repeatDate = vo.getRepeatDate();
        if(repeatDate !=null && projectVO.getWorkstopDate() !=null){
            long currworkStopDays = DateUtils.getDaysBetween(projectVO.getWorkstopDate(),repeatDate);
            projectVO.setTotworkstopDays((int) (currworkStopDays+totworkStopDays));
            Integer newToworkStopsDays = Integer.valueOf((int) (currworkStopDays+totworkStopDays));
            projectMaintain.updateWorkStopDays(pk_project,newToworkStopsDays);
        }
    }
}
